package com.qkst.website.backstage.controller;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qkst.website.backstage.bean.ResultBean;
import com.qkst.website.common.core.BaseController;
import com.qkst.website.web.bean.OrderBean;
import com.qkst.website.web.service.OrderService;

/**
 * @author lihao
 * @description 后台订单控制器
 * @date 2017-05-17
 *
 */
@Controller
@RequestMapping("/backstage/order")
public class OrderController extends BaseController{


	@Autowired
	private OrderService orderService;
	
	/**
	 * 订单列表页面数据查找
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/list.html", method = { RequestMethod.GET })
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response,OrderBean order) throws IOException{
		ModelAndView mv= this.backstageModelAndView("order/list");
		return mv;
	}
	
	/**
	 * 订单列表页面数据查找
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/list.do", method = { RequestMethod.POST })
	@ResponseBody
	public ResultBean<OrderBean> do_list(HttpServletRequest request,HttpServletResponse response,OrderBean order) throws IOException{
		ResultBean<OrderBean> result = new ResultBean<OrderBean>();
	/*	UserBean loginUser=(UserBean) request.getSession().getAttribute(SetEnums._ADMIN_USER.toString());
		order.setUser(loginUser);
		order.setUserId(loginUser.getUserId());//设置当前登陆用户
	 */		
		//分页查询订单列表
		List<OrderBean> orderList=orderService.queryOrderList(order);
		//order总记录数
		int records=orderService.queryCount(order);
		result.setRows(orderList);
		result.setPage(order.getPage());
		result.setSize(order.getRows());
		result.setRecords(records);
		return result;
	}
	
	/**创建订单
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/createOrder.do", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> createOrder(HttpServletRequest request,HttpServletResponse response,@RequestBody OrderBean order) throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
		//保存订单
		orderService.save(order,result);
		return result;
	}
	/**取消订单
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/cancelOrder.do", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> cancelOrder(HttpServletRequest request,HttpServletResponse response,OrderBean order) throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
		//取消订单
		orderService.cancelOrder(order,result);
		return result;
	}
	
	

}
